﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Services.Exceptions;
using DotNetNuke.Common.Utilities;

namespace SBPweb.DNN.PhotoGallery
{
    public partial class Settings : DotNetNuke.Entities.Modules.ModuleSettingsBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public override void LoadSettings()
        {
            try
            {
                if ((Page.IsPostBack == false))
                {
                    PhotoGalleryInfo info = PhotoGalleryController.GetPhotoGallery(null, ModuleId);
                    if (info == null)
                    {
                        info = new PhotoGalleryInfo("Photos");
                        info.ModuleId = ModuleId;
                        info.Title = "Not yet defined";
                        info.PhotoGalleryId = PhotoGalleryController.AddPhotoGallery(info);
                    }
                    txtTitle.Text = info.Title;

                    if (info.Source != null)
                    {
                        txtSource.Text = info.Source;
                        cbSource.Checked = true;
                    }
                    else
                    {
                        txtSource.Enabled = false;
                        cbSource.Checked = false;
                    }
                    
                    if (info.BasePath != null)
                    {
                        txtBasePath.Text = info.BasePath;
                        cbBasePath.Checked = true;
                    }
                    else
                    {
                        txtBasePath.Enabled = false;
                        cbBasePath.Checked = false;
                    }

                    if (info.Width != null)
                    {
                        txtWidth.Text = info.Width;
                        cbWidth.Checked = true;
                    }
                    else
                    {
                        txtWidth.Enabled = false;
                        cbWidth.Checked = false;
                    }

                    if (info.Height != null)
                    {
                        txtHeight.Text = info.Height;
                        cbHeight.Checked = true;
                    }
                    else
                    {
                        txtHeight.Enabled = false;
                        cbHeight.Checked = false;
                    }

                    if (info.Background != null)
                    {
                        txtBackground.Text = info.Background;
                        cbBackground.Checked = true;
                    }
                    else
                    {
                        txtBackground.Enabled = false;
                        cbBackground.Checked = false;
                    }

                    if (info.BorderRadius.HasValue)
                    {
                        txtBorderRadius.Text = info.BorderRadius.Value.ToString();
                        cbBorderRadius.Checked = true;
                    }
                    else
                    {
                        txtBorderRadius.Enabled = false;
                        cbBorderRadius.Checked = false;
                    }

                    if (info.ButtonBackground != null)
                    {
                        txtButtonBackground.Text = info.ButtonBackground;
                        cbButtonBackground.Checked = true;
                    }
                    else
                    {
                        txtButtonBackground.Enabled = false;
                        cbButtonBackground.Checked = false;
                    }

                    if (info.ButtonBorder != null)
                    {
                        txtButtonBorder.Text = info.ButtonBorder;
                        cbButtonBorder.Checked = true;
                    }
                    else
                    {
                        txtButtonBorder.Enabled = false;
                        cbButtonBorder.Checked = false;
                    }

                    if (info.ButtonForeground != null)
                    {
                        txtButtonForeground.Text = info.ButtonForeground;
                        cbButtonForeground.Checked = true;
                    }
                    else
                    {
                        txtButtonForeground.Enabled = false;
                        cbButtonForeground.Checked = false;
                    }

                    if (info.ButtonPlayForeground != null)
                    {
                        txtButtonPlayForeground.Text = info.ButtonPlayForeground;
                        cbButtonPlayForeground.Checked = true;
                    }
                    else
                    {
                        txtButtonPlayForeground.Enabled = false;
                        cbButtonPlayForeground.Checked = false;
                    }

                    if (info.CaptionBackground != null)
                    {
                        txtCaptionBackground.Text = info.CaptionBackground;
                        cbCaptionBackground.Checked = true;
                    }
                    else
                    {
                        txtCaptionBackground.Enabled = false;
                        cbCaptionBackground.Checked = false;
                    }

                    if (info.CaptionFont != null)
                    {
                        txtCaptionFont.Text = info.CaptionFont;
                        cbCaptionFont.Checked = true;
                    }
                    else
                    {
                        txtCaptionFont.Enabled = false;
                        cbCaptionFont.Checked = false;
                    }

                    if (info.CaptionForeground != null)
                    {
                        txtCaptionForeground.Text = info.CaptionForeground;
                        cbCaptionForeground.Checked = true;
                    }
                    else
                    {
                        txtCaptionForeground.Enabled = false;
                        cbCaptionForeground.Checked = false;
                    }

                    if (info.CaptionOpacity.HasValue)
                    {
                        txtCaptionOpacity.Text = info.CaptionOpacity.Value.ToString();
                        cbCaptionOpacity.Checked = true;
                    }
                    else
                    {
                        txtCaptionOpacity.Enabled = false;
                        cbCaptionOpacity.Checked = false;
                    }

                    if (info.ContentBackground != null)
                    {
                        txtContentBackground.Text = info.ContentBackground;
                        cbContentBackground.Checked = true;
                    }
                    else
                    {
                        txtContentBackground.Enabled = false;
                        cbContentBackground.Checked = false;
                    }

                    if (info.Foreground != null)
                    {
                        txtForeground.Text = info.Foreground;
                        cbForeground.Checked = true;
                    }
                    else
                    {
                        txtForeground.Enabled = false;
                        cbForeground.Checked = false;
                    }

                    if (info.Padding.HasValue)
                    {
                        txtPadding.Text = info.Padding.Value.ToString();
                        cbPadding.Checked = true;
                    }
                    else
                    {
                        txtPadding.Enabled = false;
                        cbPadding.Checked = false;
                    }

                    if (info.SelectedThumbnailBorder != null)
                    {
                        txtSelectedThumbnailBorder.Text = info.SelectedThumbnailBorder;
                        cbSelectedThumbnailBorder.Checked = true;
                    }
                    else
                    {
                        txtSelectedThumbnailBorder.Enabled = false;
                        cbSelectedThumbnailBorder.Checked = false;
                    }

                    if (info.TrackerFont != null)
                    {
                        txtTrackerFont.Text = info.TrackerFont;
                        cbTrackerFont.Checked = true;
                    }
                    else
                    {
                        txtTrackerFont.Enabled = false;
                        cbTrackerFont.Checked = false;
                    }

                    if (info.TrackerForeground != null)
                    {
                        txtTrackerForeground.Text = info.TrackerForeground;
                        cbTrackerForeground.Checked = true;
                    }
                    else
                    {
                        txtTrackerForeground.Enabled = false;
                        cbTrackerForeground.Checked = false;
                    }

                    if (info.AutoPlay.HasValue)
                    {
                        tcbAutoPlay.Checked = info.AutoPlay.Value;
                        cbAutoPlay.Checked = true;
                    }
                    else
                    {
                        tcbAutoPlay.Enabled = false;
                        cbAutoPlay.Checked = false;
                    }

                    if (info.CaptionVisible.HasValue)
                    {
                        tcbCaptionVisible.Checked = info.CaptionVisible.Value;
                        cbCaptionVisible.Checked = true;
                    }
                    else
                    {
                        tcbCaptionVisible.Enabled = false;
                        cbCaptionVisible.Checked = false;
                    }

                    if (info.ThumbnailsVisible.HasValue)
                    {
                        tcbThumbnailsVisible.Checked = info.ThumbnailsVisible.Value;
                        cbThumbnailsVisible.Checked = true;
                    }
                    else
                    {
                        tcbThumbnailsVisible.Enabled = false;
                        cbThumbnailsVisible.Checked = false;
                    }

                    if (info.TrackerVisible.HasValue)
                    {
                        tcbTrackerVisible.Checked = info.TrackerVisible.Value;
                        cbTrackerVisible.Checked = true;
                    }
                    else
                    {
                        tcbTrackerVisible.Enabled = false;
                        cbTrackerVisible.Checked = false;
                    }
                }
                /*
                // this needs to execute always to the client script code is registred in InvokePopupCal
                cmdCalendar.NavigateUrl = DotNetNuke.Common.Utilities.Calendar.InvokePopupCal(txtClosingDate);
                if ((Page.IsPostBack == false))
                {
                    if ((ModuleSettings["surveyclosingdate"]) == null)
                    {
                        ModuleSettings["surveyclosingdate"] = DateTime.Now.AddYears(1).ToShortDateString();
                    }
                    if ((ModuleSettings["surveytracking"]) == null)
                    {
                        ModuleSettings["surveytracking"] = 0;
                    }
                    if ((ModuleSettings["surveyresultstype"]) == null)
                    {
                        ModuleSettings["surveyresultstype"] = 0;
                    }
                    txtClosingDate.Text = Convert.ToDateTime(ModuleSettings["surveyclosingdate"]).ToShortDateString();
                    txtGraphWidth.Text = ((string)(TabModuleSettings["surveygraphwidth"]));
                    rblstPersonal.SelectedIndex = (Convert.ToInt32((ModuleSettings["surveytracking"])));
                    //rblstSurveyResults.SelectedIndex = (Convert.ToInt32((ModuleSettings["surveyresultstype"])));
                }
                */
            }
            catch (Exception exc)
            {
                // Module failed to load
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        public override void UpdateSettings()
        {
            try
            {
                bool bChanged = false;
                PhotoGalleryInfo info = PhotoGalleryController.GetPhotoGallery(null, ModuleId);
                if (txtBasePath.Enabled && info.BasePath != txtBasePath.Text)
                {
                    info.BasePath = txtBasePath.Text;
                    bChanged = true;
                }
                if (txtHeight.Enabled && info.Height != txtHeight.Text)
                {
                    info.Height = txtHeight.Text;
                    bChanged = true;
                }
                if (txtSource.Enabled && info.Source != txtSource.Text)
                {
                    info.Source = txtSource.Text;
                    bChanged = true;
                }
                if (txtTitle.Enabled && info.Title != txtTitle.Text)
                {
                    info.Title = txtTitle.Text;
                    bChanged = true;
                }
                if (txtWidth.Enabled && info.Width != txtWidth.Text)
                {
                    info.Width = txtWidth.Text;
                    bChanged = true;
                }
                if (bChanged)
                {
                    PhotoGalleryController.UpdatePhotoGallery(info);
                }
            }
            catch (Exception exc)
            {
                // Module failed to load
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void cb_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            TextBox tb = FindControl("txt" + cb.ID.Substring(2)) as TextBox;
            CheckBox tcb = FindControl("tcb" + cb.ID.Substring(2)) as CheckBox;
            if (tb != null)
            {
                tb.Enabled = cb.Checked;
            }
            if (tcb != null)
            {
                tcb.Enabled = cb.Checked;
            }
        }
}
}